package com.example.serverdemo.utils.redisutils;

import com.example.serverdemo.bean.DeviceInfoBean;
import com.example.serverdemo.common.Constant;
import com.example.serverdemo.utils.log.PrintLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Component
public class RedisUtils {
    @Autowired
    private StringRedisTemplate redisTemplate;
    public String get(final String key){
        return redisTemplate.opsForValue().get(key);
    }
    public boolean set(final String key, String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key,value);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
    public boolean getAndSet(final String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key,value);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
            e.printStackTrace();
        }
        return result;
    }
    public boolean delete(final String key){
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
    public boolean zSetAdd(String key, String value, double score){
        boolean result = false;
        try {
            redisTemplate.opsForZSet().add(key,value,score);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
    public boolean zSetDelete(String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForZSet().remove(key,value);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
    public boolean zSetQuery(String key,String value){
        boolean result = false;
        try {
            double score = redisTemplate.opsForZSet().score(key,value);
            result = true;
        }
        catch (NullPointerException e){
            PrintLogUtil.printInfo(this,"No Device Login Information Was Found");
        }
        catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return result;
    }
    public boolean setAdd(String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForSet().add(key,value);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
    public boolean setDelete(String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForSet().remove(key,value);
            result = true;
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
    public boolean setQuery(String key,String value){
        boolean result = false;
        try {
            result = redisTemplate.opsForSet().isMember(key,value);
        }catch (Exception e){
            PrintLogUtil.printError(this.getClass().getName(),e);
        }
        return result;
    }
}
